﻿using System.Collections;
using System.Collections.Generic;
using GameFramework;
using GameFramework.DataTable;
using GameFramework.UI;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace Client.UI {
    /// <summary>
    /// UI的扩展，用来根据DataTable加载对应的UI预制体
    /// 该类提供UGUI相关的辅助功能
    /// </summary>
    public static class UIExtension 
    {
        /// <summary>
        /// 检查是否存在UIForm,根据uiFormId 枚举
        /// </summary>
        /// <param name="uiComponent"></param>
        /// <param name="uiFormId"></param>
        /// <param name="uiGroupName"></param>
        /// <returns></returns>
        public static bool HasUIForm(this UIComponent uiComponent,UIFormId uiFormId,string uiGroupName = null) {
            return uiComponent.HasUIForm((int)uiFormId, uiGroupName);
        }
        /// <summary>
        /// 检查是否存在UIForm，根据UIForm ID
        /// </summary>
        /// <param name="uiComponent"></param>
        /// <param name="uiFormId"></param>
        /// <param name="uiGroupName"></param>
        /// <returns></returns>
        public static bool HasUIForm(this UIComponent uiComponent, int uiFormId, string uiGroupName = null) {
            IDataTable<DRUIForm> dtUIForm = GameEntry.DataTable.GetDataTable<DRUIForm>();
            DRUIForm drUIForm = dtUIForm.GetDataRow(uiFormId);
            if (drUIForm == null) {
                return false;
            }
            string assetName = Utility.Text.Format("{0}/{1}",drUIForm.FileName,drUIForm.AssetName);
            assetName = AssetUtility.GetUIFormAsst(assetName);
            Debug.Log(assetName);
            if (string.IsNullOrEmpty(uiGroupName)) {
                return uiComponent.HasUIForm(assetName);
            }

            IUIGroup uiGroup = uiComponent.GetUIGroup(uiGroupName);
            if (uiGroup == null) {
                return false;
            }
            return uiComponent.HasUIForm(assetName);
        }

        /// <summary>
        /// 关闭界面
        /// </summary>
        /// <param name="uiComponent"></param>
        /// <param name="uiForm"></param>
        public static void CloseUIForm(this UIComponent uiComponent, UGuiForm uiForm) {
            uiComponent.CloseUIForm(uiForm.UIForm);
        }

        public static int? OpenUIForm(this UIComponent uiComponent, UIFormId uiFormId, object usrData) {
            return uiComponent.OpenUIForm((int) uiFormId, usrData);
        }

        public static int? OpenUIForm(this UIComponent uiComponent, int uiFormId, object usrData) {
            IDataTable<DRUIForm> dtUIForm = GameEntry.DataTable.GetDataTable<DRUIForm>();
            DRUIForm drUIForm = dtUIForm.GetDataRow(uiFormId);
            if (drUIForm == null) {
                Log.Warning("Can not load UI form '{0}' from data table.", uiFormId.ToString());
                return null;
            }
            string assetName = Utility.Text.Format("{0}/{1}",drUIForm.FileName,drUIForm.AssetName);
            assetName = AssetUtility.GetUIFormAsst(assetName);
            return uiComponent.OpenUIForm(assetName, drUIForm.UIGroupName,Constant.AssetPriority.UIFormAsset,false,usrData);
        }

    }
}
